import java.util.Stack;

/**
 * Created by loso on 2017/3/17.
 */
public class _20_ValidParentheses {


    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch(c){
                case '(':
                case '{':
                case '[':stack.push(c);
                break;
                case ')':
                    if (stack.isEmpty()||stack.pop() != '('){
                        return false;
                    }
                    break;
                case '}':
                    if (stack.isEmpty()||stack.pop() != '{'){
                        return false;
                    }
                    break;
                case ']':
                    if (stack.isEmpty()||stack.pop() != '['){
                        return false;
                    }
                    break;
            }
        }
        if(stack.isEmpty()){
            return true;
        }
        return false;
    }

}
